package dev

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"jgt-wallet/src/context"
	"os"
)

// AuthCode 认证码管理
type Key struct {
}

//  检查秘钥
func (a *Key) CheckKey(ctx *context.Context) error {
	return ctx.ResOK()
}

func (a *Key) MakeKey(ctx *context.Context) error {
	key, _ := rsa.GenerateKey(rand.Reader, 2048)
	pkey := key.PublicKey
	//	private := x509.MarshalPKCS1PrivateKey(key)
	private := x509.MarshalPKCS1PrivateKey(key)

	block1 := &pem.Block{
		Type:  "RSA PRIVATE KEY",
		Bytes: private,
	}
	f1, _ := os.Create("应用私钥2048.pem")

	pem.Encode(f1, block1)

	public, _ := x509.MarshalPKIXPublicKey(&pkey)

	//	public = append(public, sh...)

	block2 := &pem.Block{
		Type:  "PUBLIC KEY",
		Bytes: public,
	}
	f2, _ := os.Create("应用公钥2048.pem")
	pem.Encode(f2, block2)

	return ctx.ResOK()
}
